package com.mvc.controller;

import com.alibaba.fastjson.JSON;
import com.mvc.entity.HomeTuijian;
import com.mvc.model.Result;
import com.mvc.service.TuijianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName TuijianController
 * @Description TODO
 * @Author Roc
 * @Date 2021/11/28 17:55
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/tuijian", produces = "text/html;charset=UTF-8")
public class TuijianController {

    @Autowired
    private TuijianService tuijianService;


    @RequestMapping(value = "/page/Search.do")
    @ResponseBody
    public String pageSearch(@RequestParam(required = false) Integer id,
                             @RequestParam(required = false) String name,
                             @RequestParam Integer page,
                             @RequestParam Integer limit
    ){
        Integer startRow = (page - 1) * limit;
        List<HomeTuijian> list = tuijianService.pageSearch(id,name,startRow,limit);
        Result result = new Result();
        result.setCode(0);
        result.setData(list);
        String json = JSON.toJSONString(result);
        return json;
    }

    @RequestMapping(value = "/tjinsert.do")
    @ResponseBody
    public String tjinsert(HomeTuijian homeTuijian){
        Integer n = tuijianService.tjinsert(homeTuijian);
        Result result = new Result();
        if (n == 1){
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1);
            result.setMsg("失败");
        }
        String json = JSON.toJSONString(result);
        return json;

    }

    @RequestMapping(value = "/tjupdate.do")
    @ResponseBody
    public String tjupdate(HomeTuijian homeTuijian){
        Integer n = tuijianService.tjupdate(homeTuijian);
        Result result = new Result();
        if (n == 1){
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1);
            result.setMsg("失败");
        }
        String json = JSON.toJSONString(result);
        return json;

    }

    @RequestMapping(value = "/tjdel.do")
    @ResponseBody
    public String tjdel(@RequestParam Integer id){
        Integer n = tuijianService.tjdel(id);
        Result result = new Result();
        if (n == 1){
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1);
            result.setMsg("失败");
        }
        String json = JSON.toJSONString(result);
        return json;

    }

    @RequestMapping(value = "/state.do")
    public String sjxjupdate(@RequestParam Integer id,
                              @RequestParam Integer state){
    tuijianService.sjxjupdate(state,id);
        Result result = new Result();
            result.setCode(0);
            result.setMsg("成功");

        String json = JSON.toJSONString(result);
        return json;
    }


}
